package com.mao.action.login;

import java.util.Date;

import javax.servlet.http.HttpSession;

import com.mao.action.BaseAction;
import com.mao.listener.OnlineUserListener;
import com.mao.model.user.User;
import com.mao.service.adminmanage.IAdminManageService;
import com.mao.service.store.IUserService;
import com.mao.util.MD5Util;
import com.mao.util.SessionHelper;
import com.mao.util.Validator;

public class LoginAction extends BaseAction {

	private IUserService<User> userService;

	private IAdminManageService adminManageService;

	private String loginName;

	private String password;

	private User user;

	private String code;

	@Override
	public String execute() {
		User sessionUser = SessionHelper.getSessionUser();
		if (Validator.isEmpty(loginName) || Validator.isEmpty(password)) {
			if (sessionUser != null) {
				user = userService.login(sessionUser.getLoginName(),
						sessionUser.getPassword());
			} else {
				errorInfo = ": - (&nbsp;&nbsp;用户名或密码不能为空!";
				return "login";
			}
		}
		if(!Validator.isEmpty(SessionHelper.getCode())){
			if (!SessionHelper.getCode().toLowerCase().equals(code.toLowerCase())) {
				user = new User();
				user.setLoginName(loginName);
				user.setPassword(password);
				errorInfo = ": - (&nbsp;&nbsp;验证码错误，请重新输入！";
				return "login";
			}
		}
		if (!Validator.isEmpty(loginName) || !Validator.isEmpty(password)) {
			String pwd = MD5Util.MD5(password);// 将输入的密码加密成MD5形式，再去跟数据库里面的MD5形式的密码去比较
			user = userService.login(loginName, pwd);
			if (user != null) {
				SessionHelper.setUser(user);
			} else {
				user = new User();
				user.setLoginName(loginName);
				user.setPassword(password);
				errorInfo = ": - (&nbsp;&nbsp;登录错误，可能是您的用户名不存在，或是您的密码不正确！";
				return "login";
			}
		}
		// 更新登录时间
		User updateUser = adminManageService.getUserById(user.getId());
		updateUser.setLoginTime(new Date());
		adminManageService.saveOrUpdateUser(updateUser);
		// 单用户登录
        //String password = request.getParameter("password");
        HttpSession session = SessionHelper.getSession();
        if (OnlineUserListener.sessionMap.get(loginName.trim()) != null && OnlineUserListener.sessionMap.get(loginName.trim()).toString().length() > 0) {
            // 当前用户已经在线 删除用户
            HttpSession sessionold = (HttpSession) OnlineUserListener.sessionMap.get(loginName.trim());
            // 注销已在线用户session
            sessionold.invalidate();
            OnlineUserListener.sessionMap.remove(loginName.trim());
            // 清除已在线用户，更新map key 当前用户 value session对象
            OnlineUserListener.sessionMap.put(loginName.trim(), session);
            OnlineUserListener.sessionMap.remove(session.getId());
        } else {
            // 根据当前sessionid 取session对象。 更新map key=用户名 value=session对象 删除map
            // key= sessionid
            OnlineUserListener.sessionMap.get(session.getId());
            OnlineUserListener.sessionMap.put(loginName.trim(),OnlineUserListener.sessionMap.get(session.getId()));
            OnlineUserListener.sessionMap.remove(session.getId());
        }
		return "main";
	}
	
	public String index() {
		return "index";
	}

	public IUserService<User> getUserService() {
		return userService;
	}

	public void setUserService(IUserService<User> userService) {
		this.userService = userService;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public IAdminManageService getAdminManageService() {
		return adminManageService;
	}

	public void setAdminManageService(IAdminManageService adminManageService) {
		this.adminManageService = adminManageService;
	}

}
